Skip to main content

344. Reverse String

Description

Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

info

You can read the full description here.

Solution 1

Approach

  1. Use list.reverse().

Implementation

class Solution:
def reverseString(self, s: List[str]) -> None:
s.reverse()

Complexity Analysis

  • nn: length of s
  • Time Complexity: O(n)O(n)
  • Space Complexity: O(1)O(1)